((window) => {
  // 定义状态值常量

  const PENDING = 'pending';
  const FULFILLED = 'fulfilled';
  const REJECTED = 'rejected';

  // 自定义Promise
  function Promise (excutor) {
    // 初始化三个属性
    // 1. 状态 pending/fulfilled/rejected
    this.state = PENDING
    // 2. 结果数据  undefined/value/reason  ==> data
    this.data = undefined
    // 3. 保存待处理回调的数组: [{resolved () {}, rejected () {}}, {resolved () {}, rejected () {}}] => callbacks
    this.callbacks = [];
    const self = this;

    /*
    resolve函数
    参数: value
    作用: 将promise的状态改为resolved, 将value保存到data
    */

    function resolve (value) {
      // 只有当前状态不为pendding, 才向下处理
      if (self.state !== PENDING) return
      self.state = FULFILLED // 改状态
      self.data = value      // 保存数据
      // 异步执行callbacks中所有成功的回调
      setTimeout(() => {
        self.callbacks.forEach(callbackObj => callbackObj.resolved(value))
      }, 0)
    }

    
    /* 
    reject函数
    参数: reason
    作用: 将promise的状态改为rejected, 将reason保存到data
    */
    function reject (reason) {
      // 只有当前状态不为pendding, 才向下处理
      if (self.state !== PENDING) return
      self.state = REJECTED // 改状态
      self.data = reason      // 保存数据
      // 异步执行callbacks中所有成功的回调
      setTimeout(() => {
        self.callbacks.forEach(callbackObj => callbackObj.rejected(reason))
      }, 0)
    }


    // 捕获执行器抛出的错误
    try {
      /*  在构造函数中, 会立即同步调用执行器函数, 并传入resolve和reject 两个函数 */
      excutor(resolve, reject);
    } catch (error) {
      // 将当前promise变为rejected, reason为error
      reject(error)
    }
  }


  /* 
  then方法 成功的回调
  简单表达: 由then指定的回调执行的结果决定
  返回promise  ==> 得到并改为它的状态
  返回非promise  ==> 变为成功状态
  抛出错误   ==> try---catch  => 变为失败状态
  */
  Promise.prototype.then = function (onResolved, onRejected) {
    const self = this
    // 判断onResolved 和 onRejected类型
    // 如果onResolved不是函数, 指定一个默认函数, 向下传递value
    onResolved = typeof onResolved === 'function' ? onResolved : value => value
    // 如果onRejected不是函数, 指定一个默认函数, 向下传递reason
    onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason }

    // 返回一个promise对象  接收一个resolve 和 reject
    return new Promise((resolve,reject) => {
      
      // 指定成功/失败的回调函数, 根据执行结果更新返回的promies的状态
      function handle (callback) {
        try {
          const result = callback(self.data)
          if (result instanceof Promise) { // 情况1: 返回promise  ==> 得到并改为它的状态
            result.then(
              value => resolve(value),
              reason => reject(reason)
            )
          } else { // 情况2: 返回非promise  ==> 变为成功状态
            resolve(result)
          }
        } catch (error) { // 情况3: 抛出错误   ==> try---catch  => 变为失败状态
          reject(error)
        }
      }

      if(self.state=== FULFILLED){
        // 如果当前是成功的, 异步执行onResolved
        setTimeout(() => {
          handle(onResolved)
        }, 0)
      }else if(self.state === REJECTED) {
        // 失败的  异步执行失败的回调
        setTimeout(() => {
          handle(onRejected)
        }, 0);
      }else{
        // 如果当前promise是pending, 保存成功和失败的回调
        self.callbacks.push({
          // resolved属性 值是对应的函数
          resolved: () => handle(onResolved),
          rejected: () =>handle(onRejected)
        })
      }
    })
  }


  Promise.prototype.catch = function (onRejected) {
    return this.then(value => value, onRejected)
  }


  // 返回一个成功的promise, value为指定的value

  Promise.resolve = function (value) {
    console.log(1);
    return new Promise((resolve, reject) => {
      if (value instanceof Promise) {
        value.then(
          value => resolve(value),
          reason => reject(reason)
        )
      } else {
        resolve(value)
      }
    })
  }

  // 返回一个失败的promise, reason为指定的reason
  Promise.reject = function (reason) {
    return new Promise((resolve, reject) => {
      reject(reason)
    })
  }


  // 向外暴露自定义的Promise
  window.Promise = Promise
})(window)